package handingIn;

import handingIn.HandingIn;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;
import org.icefaces.component.fileentry.FileEntry;
import org.icefaces.component.fileentry.FileEntryEvent;
import org.icefaces.component.fileentry.FileEntryResults;
import org.icefaces.component.fileentry.FileEntryResults.FileInfo;
import task.ExerciseSheet;
import user.User;
import user.UserAdminManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="handingInLater")
@CustomScoped(value="#{window}")
public class HandingInLaterManagedBean {
    
    private ExerciseSheet sheet;
    
    private HandingIn handingIn;
    
    private User user;
    
    private File file;
    
    private boolean succesful;
    
    public HandingInLaterManagedBean() {
    }
    
    public List<SelectItem> getSheets() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Kein Übungsblatt"));
        for (ExerciseSheet es: lecture.getExerciseSheets()) {
            list.add(new SelectItem(es, es.getName()));
        }
        return list;
    }
    
    public List<SelectItem> getHandingIns() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Keine Abgabe"));
        if (this.sheet == null) {
            for (ExerciseSheet es:lecture.getExerciseSheets()) {
                for (HandingIn h: es.getHandingIns()) {
                    list.add(new SelectItem(h,h.getName()));
                }
            }
        } else {
            for (HandingIn h: this.sheet.getHandingIns()) {
                list.add(new SelectItem(h,h.getName()));
            }
        }
        return list;
        
    }
    
    
    public List<SelectItem> getUsers() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        UserAdminManagedBean ua = c.getApplication().evaluateExpressionGet(c, "#{userAdminBean}", UserAdminManagedBean.class);
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Kein User"));
        for (User u: lecture.getStudents()) {
            list.add(new SelectItem(u, u.getUsername()));
        }
        return list;
    }

    public HandingIn getHandingIn() {
        return handingIn;
    }

    public void setHandingIn(HandingIn handingIn) {
        System.out.println("Handing In gesetzt");
        this.succesful = false;
        this.handingIn = handingIn;
    }

    public ExerciseSheet getSheet() {
        return sheet;
    }

    public void setSheet(ExerciseSheet sheet) {
        this.succesful = false;
        System.out.println("Sheet gesetzt");
        this.sheet = sheet;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        System.out.println("User gesetzt");
        this.succesful = false;
        this.user = user;
    }
    
    public void fileUploaded (FileEntryEvent event) {
        FileEntry fileEntry = (FileEntry) event.getSource();
        FileEntryResults results = fileEntry.getResults();
        for (FileInfo fileInfo : results.getFiles()) {
            if (fileInfo.isSaved()) {
                this.file = fileInfo.getFile();
            }
        }
    }
    
    public void handItIn() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        lecture.addHandingIn(this.sheet, this.user.getUsername(), this.handingIn, this.file);
        this.succesful = true;
        
    }

    public boolean isSuccesful() {
        return succesful;
    }

    public void setSuccesful(boolean succesful) {
        this.succesful = succesful;
    }
}
